package com.xxx.service.impl;

import com.xxx.service.ITokenService;
import com.xxx.util.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

@Service
public class TokenServiceImpl implements ITokenService {
    /**
     * 生成随机token保存到redis
     * key :loginId:courseId  value:token
     */

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String creatreToken(String courseId) {
        //去重排序
        String[] split = courseId.split(",");
        TreeSet<String> treeSet = new TreeSet<>();
        for (String s : split) {
            treeSet.add(s);
        }
        String handleCourseId = String.join(",", treeSet);


        //生成随机token
        String token = StrUtils.getRandomString(6);
        Long loginId = 3L; //模拟用户id   todo
        String key = loginId + ":" + courseId;
        stringRedisTemplate.opsForValue().set(key, token, 5, TimeUnit.MINUTES);
        return token;
    }
}
